const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {
  CleanWebpackPlugin
} = require('clean-webpack-plugin')

module.exports = env => {
  env = env || {};
  const devOrProd = env.production ? 'production' : 'development';
  const sourceMap = env.production ? 'none' : 'eval';
  return {
    mode: devOrProd,
    devtool: sourceMap,
    devServer: {
      contentBase: './dist'
    },
    entry: {
      app: './src/index.js',
    },
    output: {
      filename: '[name].[contenthash].js',
      path: path.resolve(__dirname, 'dist'),
      publicPath: '/'
    },
    plugins: [
      new CleanWebpackPlugin({
        cleanStaleWebpackAssets: false
      }),
      new HtmlWebpackPlugin({
        title: 'webpack'
      })
    ],
    optimization: {
      moduleIds: 'hashed',
      runtimeChunk: 'single',
      splitChunks: {
        cacheGroups: {
          vendors: {
            test: /[\\/]node_modules[\\/]/,
            name: 'vendors',
            chunks: 'all'
          }
        }
      },
    }
  }
}
